package com.example.mysmall.mvp_rajava.net;

import com.example.mysmall.mvp_rajava.config.Urls;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by xingge on 2017/8/15.
 */

public class RetrofitService {
    private static Retrofit retrofit;
    private RetrofitService(){}

    public static Api create(){
        if(retrofit == null){
            synchronized (RetrofitService.class){
                if(retrofit == null) {
//                    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
//                    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//                                .addInterceptor(loggingInterceptor)

                    OkHttpClient okHttpClient = new OkHttpClient.Builder()
                            .connectTimeout(5000, TimeUnit.MILLISECONDS)
                            .readTimeout(5000, TimeUnit.MILLISECONDS)
                            .writeTimeout(5000, TimeUnit.MILLISECONDS)
                            .retryOnConnectionFailure(true)
                            .build();
                    retrofit = new Retrofit.Builder()
                            .baseUrl(Urls.BASEURL)
                            .addConverterFactory(GsonConverterFactory.create())
                            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                            .client(okHttpClient)
                            .build();
                }
            }
        }
        return retrofit.create(Api.class);
    }
}
